package cn.cimoc.controller;

import cn.cimoc.annotation.ResponseResult;
import cn.cimoc.model.dto.output.AddressDTO;
import cn.cimoc.service.AddressService;
import cn.cimoc.util.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/addresses")
@ResponseResult
public class AddressController extends BaseController {

    @Autowired
    AddressService service;

    @PreAuthorize("hasAuthority('/address/show')")
    @GetMapping("/")
    public List<AddressDTO> getAddress() {
        return service.getAddress(uid);
    }

    @PreAuthorize("hasAuthority('/address/update')")
    @PostMapping("/{aid}/set_default")
    public void setDefault(@PathVariable("aid") Long aid) {
        service.setDefault(aid, uid);
        HttpStatus.success();
    }

    @PreAuthorize("hasAuthority('/address/delete')")
    @PostMapping("/{aid}/delete")
    public void delete(@PathVariable("aid") Long aid) {
        service.delete(aid);
        HttpStatus.success();
    }


    @PreAuthorize("hasAuthority('/address/add')")
    @PostMapping("/addnew")
    public void addNew(String receiver,
                       String provinceCode,
                       String cityCode,
                       String areaCode,
                       String zip,
                       String address,
                       String phone,
                       String tel,
                       String tag) {
        service.add(receiver, provinceCode, cityCode, areaCode, zip, address, phone, tel, tag, tUsername, uid);
        HttpStatus.success();
    }
}
